package org.wmq.travelagency02.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wmq.travelagency02.constant.RoleConstant;
import org.wmq.travelagency02.helper.JwtLoginHelper;
import org.wmq.travelagency02.model.DTO.LoginDTO;
import org.wmq.travelagency02.model.Entity.CacheUserInfo;
import org.wmq.travelagency02.model.Entity.Guidebook;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.model.Entity.Traveller;
import org.wmq.travelagency02.service.GuidebookService;
import org.wmq.travelagency02.service.TravellerService;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user/login")
@RequiredArgsConstructor
public class LoginController {
    private final TravellerService travellerService;
    private final GuidebookService guidebookService;
    private final StringRedisTemplate redisTemplate;

    @PostMapping("/traveller")
    public Result<String> travellerLogin(@RequestBody LoginDTO loginDTO){
        Traveller traveller = this.travellerService.lambdaQuery().eq(Traveller::getName, loginDTO.getName())
                .eq(Traveller::getPassword, loginDTO.getPassword()).select(Traveller::getId).one();
        if (traveller == null || traveller.getId() == null){
            return Result.error("用户名或密码错误");
        }
        CacheUserInfo cacheUserInfo = new CacheUserInfo(traveller.getId(), RoleConstant.TRAVELLER);
        String jwtToken = JwtLoginHelper.createJwtToken(loginDTO.getName());
        this.redisTemplate.opsForValue().set(jwtToken, JSON.toJSONString(cacheUserInfo));
        return Result.ok(jwtToken);
    }
    @PostMapping("/traveller/register")
    public Result<Void> register(@RequestBody Traveller traveller){
        Long count = this.travellerService.lambdaQuery().eq(Traveller::getName, traveller.getName()).count();
        if (count != 0){
            return Result.error("用户名已被注册");
        }
        traveller.setUpdateTime(LocalDateTime.now());
        traveller.setCreateTime(LocalDateTime.now());
        this.travellerService.save(traveller);
        return Result.ok();
    }

    @PostMapping("/guidebook")
    public Result<String> guidebookLogin(@RequestBody LoginDTO loginDTO){
        LambdaQueryWrapper<Guidebook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Guidebook::getName, loginDTO.getName()).
                eq(Guidebook::getPassword, loginDTO.getPassword())
                .select(Guidebook::getId);
        Guidebook guidebook = this.guidebookService.getOne(queryWrapper);
        if (guidebook == null || guidebook.getId() == null){
            return Result.error("用户名或密码错误");
        }
        CacheUserInfo cacheUserInfo = new CacheUserInfo(guidebook.getId(), RoleConstant.GUIDEBOOK);
        String jwtToken = JwtLoginHelper.createJwtToken(loginDTO.getName());
        this.redisTemplate.opsForValue().set(jwtToken, JSON.toJSONString(cacheUserInfo));
        return Result.ok(jwtToken);
    }

    @PostMapping("guidebook/register")
    public Result<Void> guidebookRegister(@RequestBody Guidebook guideBook){
        Long count = this.guidebookService.lambdaQuery().eq(Guidebook::getName, guideBook.getName()).count();
        if (count != 0){
            return Result.error("用户名已被注册");
        }
        guideBook.setCreateTime(LocalDateTime.now());
        guideBook.setUpdateTime(LocalDateTime.now());
        this.guidebookService.save(guideBook);
        return Result.ok();
    }
}
